﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GraphicsFramework.Support
{
    class CopyPaste
    {
        private List<Drawing.GraphicBase> board;
        private Entity.Point2F location;
        private bool onarray;
        private float rotation;
        private Entity.Point2F center;

        public CopyPaste()
        {
            board = new List<Drawing.GraphicBase>();
        }

        public void Copy(List<Drawing.GraphicBase> selected)
        {
            board.Clear();
            rotation = 0;
            location = new Entity.Point2F(0.5F, 0.5F);

            foreach (Drawing.GraphicBase draw in selected)
            {
                board.Add(draw.Clone());
            }

            UpdateCenter();
        }

        public void UpdateCenter()
        {
            if (board.Count == 0)
                center = new Entity.Point2F(0, 0);
            else if (board.Count == 1)
                //If there is only one drawing selection, get its center.
                center = board[0].Center.Clone();
            else
            {
                //Calculate the center base of the boundary of all selected drawings.
                Entity.Rectangle2F bounds = GetOriginalDrawingsBounds();
                center = new Entity.Point2F((bounds.Point1.X + bounds.Point2.X) / 2,
                                            (bounds.Point1.Y + bounds.Point2.Y) / 2);
            }
        }

        private Entity.Rectangle2F GetOriginalDrawingsBounds()
        {
            if (board.Count != 0)
            {
                Entity.Rectangle2F bounds = null;
                Entity.Rectangle2F other = null;

                foreach (Drawing.GraphicBase drawing in board)
                {
                    //Check if bounds is null
                    if (bounds != null)
                    {
                        other = drawing.GetOriginalBounds();

                        //Check if drawing bound is null.
                        //Merge to the existing bounds if not null.
                        if (other != null)
                            bounds.Merge(other);
                    }
                    else
                    {
                        bounds = drawing.GetOriginalBounds();
                    }
                }

                return bounds;
            }

            return null;
        }

        public List<Drawing.GraphicBase> Board
        {
            get { return board; }
            set { board = value; }
        }

        public Entity.Point2F Location
        {
            get { return location; }
            set { location = value; }
        }

        public bool OnArray
        {
            get { return onarray; }
            set { onarray = value; }
        }

        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        public Entity.Point2F Center
        {
            get { return center; }
        }

    }
}
